<?php

class Gmapsprofile_View_Helper_GmapsprofileCompose extends Zend_View_Helper_Abstract
{
  public function gmapsprofileCompose($subject, $label = false, $viewer = null)
  {
  	$class = $label ? 'buttonlink' : 'buttonlink_members_gmap';
	$label = !$label ? '' : $this->view->translate('Send Message');
    // Not logged in
    $viewer = Engine_Api::_()->user()->getViewer();

    if( !$viewer->getIdentity() || $viewer->getGuid(false) === $subject->getGuid(false) ) {
      return false;
    }

    // Get setting?
    $permission = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'messages', 'create');
    if( Authorization_Api_Core::LEVEL_DISALLOW === $permission )
    {
      return false;
    }
    $messageAuth = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'messages', 'auth');
    if( $messageAuth == 'none' ) {
      return false;
    } else if( $messageAuth == 'friends' ) {
      // Get data
      $direction = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('user.friends.direction', 1);
      if( !$direction ) {
        //one way
        $friendship_status = $viewer->membership()->getRow($subject);
      }
      else $friendship_status = $subject->membership()->getRow($viewer);

      if( $friendship_status && $friendship_status->active == 0 ) {
        return false;
      }
    }
	//if use Advanced Mail
	$settings = Engine_Api::_()->getApi('settings', 'core');
	$table = Engine_Api::_()->getDbtable('recipients', 'messages');
    $sql = "SHOW TABLES LIKE '%advancedmail_ads%'";
    $table_name = $table->getAdapter()->query($sql)->fetch();
	if ($table_name && (int)$settings->advancedmail_enabled) {
	  return $this->view->htmlLink(array('route' => 'advancedmail_general', 'action' => 'compose', 'to' => $subject->getIdentity()), $label, array('class' => $class . ' icon_compose', 'title' => $this->view->translate('Send Message'), 'onclick' => "parent.Smoothbox.open(this.href); return false;"));    	
	}	
    return $this->view->htmlLink(array('route' => 'messages_general', 'action' => 'compose', 'to' => $subject->getIdentity()), $label, array('class' => $class . ' icon_compose', 'title' => $this->view->translate('Send Message')));    
  }  
}